#include "stdafx.h"
#include "CodeListAdapter.h"
#include "fmt/format.h"
#include "CommonUtil.h"
#include "base/util/string_number_conversions.h"

CodeListAdapter::CodeListAdapter() {
    m_colNames.push_back(L"lb_price");
    m_colNames.push_back(L"lb_zd");
    m_colNames.push_back(L"lb_zdf");
    m_colNames.push_back(L"lb_bid");
    m_colNames.push_back(L"lb_ask");
    m_colNames.push_back(L"lb_prev_close");
    m_colNames.push_back(L"lb_open");
    m_colNames.push_back(L"lb_high");
    m_colNames.push_back(L"lb_low");
    m_colNames.push_back(L"lb_vol");
    m_colNames.push_back(L"lb_amt");
    m_colNames.push_back(L"exx");
}

CodeListAdapter::~CodeListAdapter() {

}

int CodeListAdapter::getCount() {
    return m_goodsList.size();
}

void CodeListAdapter::getView(int position, SOUI::SWindow *pItem, pugi::xml_node xmlTemplate) {
    if (pItem->GetChildrenCount() == 0) {
        pItem->InitFromXml(xmlTemplate);
    }
    
    if (position >= (int)m_goodsList.size()) {
        return;
    }
    
    auto pGoods = m_goodsList[position];
    if (pGoods) {
        updateText(pItem, L"lb_price", pGoods, FID_PRICE);
        updateText(pItem, L"lb_zd", pGoods, FID_ZHANGDIE);
        updateText(pItem, L"lb_zdf", pGoods, FID_ZDF);
        updateText(pItem, L"lb_vol", pGoods, FID_VOLUME);
        updateText(pItem, L"lb_amt", pGoods, FID_AMOUNT);
        updateText(pItem, L"lb_bid", pGoods, FID_BUY_1);
        updateText(pItem, L"lb_ask", pGoods, FID_SELL_1);
        updateText(pItem, L"lb_prev_close", pGoods, FID_PREV_CLOSE);
        updateText(pItem, L"lb_open", pGoods, FID_OPEN);
        updateText(pItem, L"lb_high", pGoods, FID_HIGH);
        updateText(pItem, L"lb_low", pGoods, FID_LOW);
    }
    
}

SOUI::SStringW CodeListAdapter::GetColumnName(int iCol) const {
    if (iCol < (int)m_colNames.size()) {
        return m_colNames[iCol];
    }
    return L"";
}

std::vector<SOUI::SStringW> CodeListAdapter::GetColumnNames() {
    return m_colNames;
}

void CodeListAdapter::setColumnNames(const std::vector<SOUI::SStringW> &names) {
    m_colNames = names;
}

void CodeListAdapter::updateText(SOUI::SWindow *pItem, const SOUI::SStringW &ctrlName, GoodsPtr pGoods, int fid) {

    if (pGoods) {
        auto pCtrl = pItem->FindChildByName(ctrlName);
        if (pCtrl) {
        
            auto fV = pGoods->getStrValue(fid);
            pCtrl->SetWindowText(fV.text);
            pCtrl->SetAttribute(L"colorText", fV.color);
        }
    }
    
}
